class PreloadGroup {
	private loadUI: any;
	public constructor(loadUI: any) {
		this.loadUI = loadUI;
	}

	public loadGroup() {
		var point = this;
		return new Promise(function (resolve, reject) {
			/**
			 * preload资源组加载完成
			 * preload resource group is loaded
			 */
			function onResourceLoadComplete(event: RES.ResourceEvent): void {
				if (event.groupName == "preload") {
					RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, onResourceLoadComplete, this);
					RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
					RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
					RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
					resolve();
				}
			}
			
			RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, onResourceLoadComplete, point);
			RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, point.onResourceLoadError, point);
			RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, point.onResourceProgress, point);
			RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, point.onItemLoadError, point);
			RES.loadGroup("preload");
		});

	}

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
	private onItemLoadError(event: RES.ResourceEvent): void {
		console.warn("Url:" + event.resItem.url + " has failed to load");
	}
    /**
     * 资源组加载出错
     * Resource group loading failed
     */
	private onResourceLoadError(event: RES.ResourceEvent): void {
		//TODO
		console.warn("Group:" + event.groupName + " has failed to load");
		//忽略加载失败的项目
		//ignore loading failed projects
		// this.onResourceLoadComplete(event);
	}
    /**
     * preload资源组加载进度
     * loading process of preload resource
     */
	private onResourceProgress(event: RES.ResourceEvent): void {
		if (event.groupName == "preload") {
			this.loadUI.setProgress(event.itemsLoaded, event.itemsTotal);
		}
	}
}